package edu.cuit.campus.validation;

import edu.cuit.campus.po.Employee;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * 自定义业务逻辑校验——员工最低薪资不能低于对应职位的最低薪资要求
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/19 9:13
 */
public class EmployeeValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return clazz == Employee.class;
    }

    @Override
    public void validate(Object target, Errors errors) {
        var emp = (Employee) target;
        if(emp.getSalary() < emp.getJob().getLowSalary()){
            errors.rejectValue("salary", "low.salary", String.format("薪资不能低于%s的最低薪资%d", emp.getJob().getValue(), emp.getJob().getLowSalary()));
        }
    }
}
